package me.zhengjie.modules.serviceprovider.service.impl;

import me.zhengjie.modules.serviceprovider.domain.TServiceproviderOrderAppraise;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.modules.serviceprovider.repository.TServiceproviderOrderAppraiseRepository;
import me.zhengjie.modules.serviceprovider.service.TServiceproviderOrderAppraiseService;
import me.zhengjie.modules.serviceprovider.service.dto.TServiceproviderOrderAppraiseDTO;
import me.zhengjie.modules.serviceprovider.service.dto.TServiceproviderOrderAppraiseQueryCriteria;
import me.zhengjie.modules.serviceprovider.service.mapper.TServiceproviderOrderAppraiseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

/**
* @author xuyukun
* @date 2019-08-25
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TServiceproviderOrderAppraiseServiceImpl implements TServiceproviderOrderAppraiseService {

    @Autowired
    private TServiceproviderOrderAppraiseRepository tServiceproviderOrderAppraiseRepository;

    @Autowired
    private TServiceproviderOrderAppraiseMapper tServiceproviderOrderAppraiseMapper;

    @Override
    public Object queryAll(TServiceproviderOrderAppraiseQueryCriteria criteria, Pageable pageable){
        Page<TServiceproviderOrderAppraise> page = tServiceproviderOrderAppraiseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tServiceproviderOrderAppraiseMapper::toDto));
    }

    @Override
    public Object queryAll(TServiceproviderOrderAppraiseQueryCriteria criteria){
        return tServiceproviderOrderAppraiseMapper.toDto(tServiceproviderOrderAppraiseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public TServiceproviderOrderAppraiseDTO findById(Integer id) {
        Optional<TServiceproviderOrderAppraise> tServiceproviderOrderAppraise = tServiceproviderOrderAppraiseRepository.findById(id);
        ValidationUtil.isNull(tServiceproviderOrderAppraise,"TServiceproviderOrderAppraise","id",id);
        return tServiceproviderOrderAppraiseMapper.toDto(tServiceproviderOrderAppraise.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TServiceproviderOrderAppraiseDTO create(TServiceproviderOrderAppraise resources) {
        return tServiceproviderOrderAppraiseMapper.toDto(tServiceproviderOrderAppraiseRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TServiceproviderOrderAppraise resources) {
        Optional<TServiceproviderOrderAppraise> optionalTServiceproviderOrderAppraise = tServiceproviderOrderAppraiseRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalTServiceproviderOrderAppraise,"TServiceproviderOrderAppraise","id",resources.getId());

        TServiceproviderOrderAppraise tServiceproviderOrderAppraise = optionalTServiceproviderOrderAppraise.get();
        //因为是关联表是服务工单的评价信息，先判断是否有关联对象的id
        //如果没有执行插入操作，如果有执行更新操作
        // 此处需自己修改
        resources.setId(tServiceproviderOrderAppraise.getId());
        tServiceproviderOrderAppraiseRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        tServiceproviderOrderAppraiseRepository.deleteById(id);
    }
}